package com.iflytek.universitymanagement.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytek.universitymanagement.domain.Competition;
import com.iflytek.universitymanagement.domain.CompetitionParticipationDTO;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.domain.StudentCompetition;
import com.iflytek.universitymanagement.mapper.CompetitionMapper;
import com.iflytek.universitymanagement.mapper.StudentCompetitionMapper;
import com.iflytek.universitymanagement.mapper.StudentMapper;
import com.iflytek.universitymanagement.service.CompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CompetitionServiceImpl extends ServiceImpl<CompetitionMapper, Competition> implements  CompetitionService {

    @Autowired
    private CompetitionMapper competitionMapper;
    @Autowired
    private StudentCompetitionMapper studentCompetitionMapper;

    @Autowired
    private StudentMapper studentMapper; // 假设你有 Student 表

    @Override
    public Competition getById(Long competitionId) {
        return competitionMapper.selectById(competitionId);
    }



    @Override
    public Page<CompetitionParticipationDTO> getAllStudentCompetitionInfoWithPagination(int pageNum, int pageSize) {
        // 创建分页对象
        Page<StudentCompetition> page = new Page<>(pageNum, pageSize);

        // 分页查询 student_competition 表
        Page<StudentCompetition> studentCompetitionPage = studentCompetitionMapper.selectPage(page, null);

        // 构建返回的 DTO 分页对象
        Page<CompetitionParticipationDTO> dtoPage = new Page<>();
        dtoPage.setCurrent(studentCompetitionPage.getCurrent());
        dtoPage.setSize(studentCompetitionPage.getSize());
        dtoPage.setTotal(studentCompetitionPage.getTotal());

        // 封装数据
        List<CompetitionParticipationDTO> dtoList = studentCompetitionPage.getRecords().stream().map(sc -> {
            Competition competition = competitionMapper.selectById(sc.getCompetitionId());
            Student student = studentMapper.selectById(sc.getStudentId());

            // 检查空值
            if (competition == null || student == null) {
                return null;
            }

            // 封装 DTO
            CompetitionParticipationDTO dto = new CompetitionParticipationDTO();
            dto.setCompetitionName(competition.getName());
            dto.setDescription(competition.getDescription());
            dto.setStartDate(competition.getStartDate());
            dto.setEndDate(competition.getEndDate());
            dto.setStudentName(student.getName());
            dto.setParticipationStatus(sc.getParticipationStatus());
            dto.setAwards(sc.getAwards());
            return dto;
        }).filter(dto -> dto != null).collect(Collectors.toList());

        dtoPage.setRecords(dtoList);
        return dtoPage;
    }
}


